jQuery(document).ready(function() {
	jQuery("#Barangay, #City, #Pcode").select2("enable",false);
	
	jsonurl = baseurl + "postalcode/";
	jQuery("#s2id_Pcode .select2-choice .select2-arrow b").css("background", "none"); // Auto hide arrow background in postal code
	postalIds = getCookie("tiangge_postalIds");
    if(postalIds.length>4){
		var usertype = getCookie("tiangge_usertype");
		var mobtel = getCookie("tiangge_mobtel");
	    if(usertype=="globe" || mobtel=="0"){
			editPostalMode = true;
			ids = postalIds.split("|");
			provinceId = ids[0];
			cityId = ids[1];
			brgyId = ids[2];
			zipId = ids[3];
			jQuery("select#Province").populateProvince(provinceId);
			jQuery("select#City").populateTown(provinceId, cityId);
			if(provinceId==178) {
				jQuery("select#Barangay").populateBrgy(cityId, brgyId);
			} else {
				jQuery("#areabrgy").hide();
				//jQuery("#Barangay").removeClass("error");
				jQuery("#s2id_Barangay").removeClass("error");
				jQuery("#s2id_Barangay a").css("border","2px solid #b9babe");
			}
			jQuery("select#Pcode").populatePostal(cityId, brgyId, zipId);
		}else{
			jQuery(".profileBox, .storeBox").removeClass("active");
			jQuery("p.pnone").show();
			jQuery('#pname, #pemail, #pdob, #padd').hide();
			jQuery('#globestore').text("None");
			provinceId = 0;
			cityId = 0;
			brgyId = 0;
			zipId = 0;
			editPostalMode = false;
			jQuery("select#Province").populateProvince(provinceId);
	    }
	} else {
		provinceId = 0;
		cityId = 0;
		brgyId = 0;
		zipId = 0;
		editPostalMode = false;
		jQuery("select#Province").populateProvince(provinceId);
	}

	//console.log("postalIds - " + provinceId + " " + cityId + " " + brgyId + " " + zipId);
	
    jQuery("select#Province").change(function () {
		editPostalMode = false;
		var $Province = $('#Province').val();
		if ($.trim($Province).length == 0) {
			jQuery("#s2id_Province").addClass("error");	
			jQuery("#s2id_Province a").css("border","2px solid #ff0000");
		} else {
			jQuery("#s2id_Province").removeClass("error");
			jQuery("#s2id_Province a").css("border","2px solid #b9babe");
		}
		
		jQuery("select").clearAll();
		if(jQuery(this).hasClass("disable")) {
			return;
		} else {
			province_id = jQuery(this).val();
			
			jQuery("select#City").populateTown(province_id, cityId);
		} 
		return false;
	});	

    jQuery("select#City").change(function () {
		var $City = $('#City').val();
		if ($.trim($City).length == 0) {
			jQuery("#s2id_City").addClass("error");	
			jQuery("#s2id_City a").css("border","2px solid #ff0000");	
		} else { 
			jQuery("#s2id_City").removeClass("error");
			jQuery("#s2id_City a").css("border","2px solid #b9babe");	
		}
		
		jQuery("select#Barangay").clearDropdown();
		jQuery("select#Pcode").clearDropdown();
		
		if(jQuery(this).hasClass("disable")) {
			return;
		} else {
			province_id = jQuery("select#Province").val();
			city_id = jQuery("select#City").val();
			
			if(province_id==178) {
				// Metro Manila 178
				// Populate Barangay
				jQuery("#Barangay").populateBrgy(city_id, 0);
			} else {
				// Disable Barangay
				jQuery("select#Barangay").addClass("disable");
				jQuery("select#Pcode").populatePostal(city_id, 0, zipId);
			}		
		}
		return false;
	});	

    jQuery("select#Barangay").change(function () {
		var $Barangay = $('#Barangay').val();
		if ($.trim($Barangay).length == 0) {
			jQuery("#s2id_Barangay").addClass("error");	
			jQuery("#s2id_Barangay a").css("border","2px solid #ff0000");	
		} else { 
			jQuery("#s2id_Barangay").removeClass("error"); 
			jQuery("#s2id_Barangay a").css("border","2px solid #b9babe");	
		}
		
		jQuery("select#Pcode").clearDropdown();
		
		if(jQuery(this).hasClass("disable")) {
			return;
		} else {
			jQuery("select#Pcode").populatePostal(jQuery("select#City").val(), jQuery("select#Barangay").val(), zipId);
		}
		return false;
	});	
	
	jQuery("select#Pcode").click(function() {
		//alert("this is a test. please disregard"); console.log("clicked si pcode");
		jQuery("html, body").animate({ scrollTop: jQuery(document).height()-jQuery(window).height() });
		return false;
	});
	
});	

function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) 
  {
  var c = ca[i].replace(/\s/g, "");
  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
  return "";
}

   //Sort Array by Field Name
   var sort_by = function(field, reverse, primer){
   var key = primer ? 
       function(x) {return primer(x[field])} : 
       function(x) {return x[field]};

   reverse = [-1, 1][+!!reverse];

   return function (a, b) {
       return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
     } 
   }
   
/* Reusable dropdown related methods */
jQuery.fn.extend({

	// Clear specific dropdown item
	clearDropdown: function() {
		jQuery(this).select2('data', null).addClass("disable");
		jQuery("option", this).remove();
		return this;
	},
	
	// Clear all dropdown options
	clearAll: function() {
		jQuery("select#City").clearDropdown();
		jQuery("select#Barangay").clearDropdown();
		jQuery("select#Pcode").clearDropdown();
		return this;
	},
	
	populateProvince: function(provinceId) {
		//console.log("populateProvince: " + provinceId);
		jQuery.getJSON(jsonurl + "get_province",{ajax: 'true'}, function(j){
		    j.sort(sort_by('name', true, function(a){return a.toUpperCase()}));
			var li = '<option></option>';

			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(provinceId!=0 && provinceId==id_ && editPostalMode) li += ' selected ';
				li += '>' + j[i].name + '</option>';
			}
			//Store laman sa ul li
			jQuery("select#Province").html(li)
			jQuery("select#Province").removeClass("disable");
			select_value = jQuery("#Province").select2("val");
			jQuery("select#Province").select2('val', select_value);
			return this;
		});
	},
	
	populateTown: function(provinceId, cityId) {
		//console.log("populateTown: " + provinceId + " " + cityId);
		jQuery.getJSON(jsonurl + "get_town/" + provinceId,{ajax: 'true'}, function(j){
			var li = '<option></option>';

			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(cityId!=0 && cityId==id_ && editPostalMode) li += ' selected ';
				li += '>' + j[i].name + '</option>';
			} 
			//Store laman sa ul li
			if(li.length > 1) {
				jQuery("select#City, #s2id_City").removeClass("disable");
				jQuery("#City").select2("enable",true); //edited by John
				jQuery("select#City").html(li);
			} else {
				jQuery("select#City, #s2id_City").addClass("disable");
				jQuery("#City").select2("enable",false);
			}
			
			select_value = jQuery("#City").select2("val");
			jQuery("select#City").select2('val', select_value);			
			return this;
		});
	},
	
	populateBrgy: function(cityId, brgyId) {
		jQuery.getJSON(jsonurl + "get_barangay/" + cityId,{ajax: 'true'}, function(j){
			var li = '<option></option>';

			for (var i = 0; i < j.length; i++) {
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				if(brgyId!=0 && brgyId==id_ && editPostalMode) li += ' selected ';
				li += '>' + j[i].name + '</option>';				
			}
			//Store laman sa ul li
			if(li.length > 1) {
				jQuery("select#Barangay, #s2id_Barangay").removeClass("disable");
				jQuery("#Barangay").select2("enable",true);
				jQuery("select#Barangay").html(li);
			} else {
				jQuery("select#Barangay, #s2id_Barangay").addClass("disable");
				jQuery("#Barangay").select2("enable",false);
			}

			select_value = jQuery("select#Barangay").select2("val");
			jQuery("select#Barangay").select2('val', select_value);				
			return this;
		});	
	},	
	
	populatePostal: function(city_id, barangay_id, postal_id) {			
		jQuery.getJSON(jsonurl + "get_pcode/" + city_id + "/" + barangay_id,{ajax: 'true'}, function(j){
			var li = '<option></option>';
			for (var i = 0; i < j.length; i++) {	
				id_ = j[i].id_;
				li += '<option value="' + id_ + '"';
				//if(postal_id!=0 && postal_id==id_ && editPostalMode) 
				li += ' selected ';
				li += '>' + j[i].code_ + '</option>';				
			}
			//Store laman sa ul li
			if(li.length > 1) {
				jQuery("select#Pcode, #s2id_Pcode").removeClass("disable error");
				jQuery("#s2id_Pcode a").css({"border":"2px solid #b9babe","background-color":"#FFF"});	
				jQuery("#s2id_Pcode .select2-choice .select2-arrow b").css("background-color","#FFF");
				jQuery("select#Pcode").html(li);
			} else {
				jQuery("select#Pcode, #s2id_Pcode").addClass("disable");
				jQuery("#s2id_Pcode a").css("border","2px solid #ff0000");	
			}
			
			select_value = jQuery("select#Pcode").select2("val");
			jQuery("select#Pcode").select2('val', select_value);				
			return this;
		});		
	},
	
	isAndroid: function() {
		var ua = navigator.userAgent.toLowerCase();
		if(ua.indexOf("android") > -1) {
		
		
		}
	},
	
	
});